<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>${title}</title>
<link rel="stylesheet" type="text/css" href="<c:url value='/resource/css/common.css'/>" />
<link rel="stylesheet" type="text/css" href="<c:url value='/resource/css/jquery.autocomplete.css'/>"/>
<script type="text/javascript" src="<c:url value='/resource/js/jquery-1.7.2.min.js'/>"></script>
<script type="text/javascript" src="<c:url value='/resource/js/jquery.form.js'/>"></script>
<script type="text/javascript" src="<c:url value='/resource/artdialog/artdialog.js?skin=${sys_theme}'/>"></script>
<script type="text/javascript" src="<c:url value='/resource/artdialog/iframetools.js'/>"></script>
<script type="text/javascript" src="<c:url value='/resource/js/jquery.autocomplete.js'/>"></script>	
<script type="text/javascript" src="<c:url value='/resource/js/common.js'/>"></script>
<script type="text/javascript">
	function tipclose(content,pageIndex) {
		art.dialog.tips(content, 1.5);
		window.setTimeout(function() {
			window.parent.frames["ifrm_data"].frames["ifrm_deptlist"]._goto(pageIndex);
			//刷新树节点
			window.parent.frames["ifrm_data"].refreshTree('${dept.deptId }', 'add');
			art.dialog.close();
		}, 1500);
	}
	function submitForm(pageIndex) {
	   	if($("#overDistrictName").val()==""){
			$("#overDistrictId").val("");
			 _tmpDwName="";
			}
	
		var name = $.trim($("#name").val());
		var alias = $.trim($("#alias").val());
		if ($("#shortId").val() == "" || $("#shortId").val().length != 2) {
			art.dialog.tips("请输入2位的部门编码", 1.5);
			return false;
		}
		if (name == "") {
			art.dialog.tips("部门名称不能为空", 1.5);
			return false;
		}
		if (alias == "") {
			art.dialog.tips("部门简称不能为空", 1.5);
			return false;
		}
		if ($.trim($("#jpin").val()) == "") {
			art.dialog.tips("部门简拼不能为空", 1.5);
			return false;
		}
		if (_tmpDwName != $("#overDistrictName").val()) {
			art.dialog.tips("输入驻外单位不存在！");
			$("#overDistrictName").focus();
			$("#overDistrictName").select();
			return false;
		}
		var deptId = $("#prefix").text() + $("#shortId").val() + $("#suffix").text();
		$("#alias").val(alias);
		$("#name").val(name);
		$("#hid_name").val(name);
		$("#longId").val(deptId);
		top.mask();
		$("#query_form").ajaxSubmit({
			type : "post",
			dataType : "json",
			params:{name:$("#name").val()},
			success : function(data) {
				top.unmask();
				switch (parseInt(data.success)) {
				case 1:
					tipclose("新增成功",pageIndex);
					break;
				case 2:
					art.dialog.tips("新增失败，编号重复", 1.5);
					break;
				case 3:
					art.dialog.tips("新增失败，名称或简称重复", 1.5);
					break;
				default:
					art.dialog.tips("新增失败！", 1.5);
					break;
				}
			},
			error : function() {
				top.unmask();
				art.dialog.tips("新增失败！", 1.5);
			}
		});
	}
	$(function() {
		if ("${dept.deptId}" != "320500") {
			var deptId = '${dept.deptId }';
			var deep = '${dept.deep }';
			var parent = deptId.substring(0, deep * 2 + 4);
			$("#prefix").text(parent);
			var length = 12 - parent.length - 2;
			for ( var i = 1; i <= length; i++) {
				$("#suffix").append("0");
			}
		} else {
			$("#prefix").text("320500");
			$("#suffix").text("0000")
		}
		$(":radio").bind("change", function() {
			if ($(this).val() == "0") {
				$("#prefix").text("320500");
				$("#suffix").text("0000")
				$("#deep").val("2");
			} else {
				$("#prefix").text("3205");
				$("#suffix").text("000000");
				$("#deep").val("1");
			}
		});
		/**部门自动补全*/
		$("#overDistrictName").autocomplete("<c:url value='/dept/autocomplete.json'/>",{  
		  	minChars: 1,  
		  	max : 10, // 最大显示条数
		    autoFill: false,  
		    dataType : "json",  //指定数据类型的渲染方式  
		    extraParams:{  
		    	jpin:function(){  
			    	return $("#overDistrictName").val();//url的参数传递  
			    }
			},  
			parse: function(data){  
			    var rows = [];  
			    var d = data.depts;
			    if(d){
				    for(var i=0; i<d.length; i++){  
				    	rows[rows.length] = {  
					       	 data:d[i],  
					         value:d[i],  
					         result:d[i].name  
				        };  
				    }  
			    }  
			    return rows;  
			},  
			formatItem: function(row,i,n) {  
			    return row.name+"("+row.deptId+")";    
			}  
		  }).result (function(event, data, formatted) {  
		   	$("#overDistrictId").val(data.deptId);  
		   	_tmpDwName = data.name;
		  });
	});
	var _tmpDwName="";
</script>
</head>


<body>
	<c:choose>
		<c:when test="${empty dept}">上级部门不可为空,请返回选择！</c:when>
		<c:otherwise>
			<div id="addContent" style="margin: 0;padding: 0">
				<form method="post" id="query_form" action="dept/add.json" style="margin:0px;padding:0px;width:340px;">
					<table>
						<tr　style="height:25px;">
							<td class="td_tit" style="">上级部门:</td>
							<td>${dept.name}</td>
						</tr>
						<tr style="height:25px;">
							<td class="td_tit">部门名称:</td>
							<td>
							<input type="hidden" value="12345" name="name" id="hid_name">
								<input type="text" id="name"　name="parentName"　size="35" maxlength="32" style="width:250px;"　onblur="this.value=this.value.replace(/\s/g,'');"></input><span style="color:red">*</span>
							</td>
						</tr>
						
						<c:if test="${'320500' eq dept.deptId}">
						<tr style="height:25px;">
							<td class="td_tit">直属部门:</td>
							<td>
								<input checked="checked" style="border:none;line-height:30px;width:15px;" type="radio" name="isZS" value="0" />是
								<input type="radio" name="isZS"  style="border:none;line-height:30px;width:15px;" value="1" />否　(是否市局直属)
							</td>
						</tr>
						</c:if>
						<tr style="height:25px;">
							<td class="td_tit">部门编码:</td>
							<td><span id="prefix"></span><input size="2"
								style="width:18px" maxlength="2" id="shortId"
								onblur="this.value=this.value.replace(/[\W]/g,'')" type="text"></input><span
								id="suffix"></span><span style="color:red">*</span>
							</td>
						</tr>
						<tr style="height:25px;">
							<td class="td_tit">部门简称:</td>
							<td><input size="35" maxlength="32" style="width:250px;"
								onblur="this.value=this.value.replace(/\s/g,'');" id="alias"
								type="text" name="alias"></input><span style="color:red">*</span>
							</td>
						</tr>
						<tr style="height:25px;">
							<td class="td_tit">部门简拼:</td>
							<td><input size="35" maxlength="12" style="width:250px;"
								onblur="this.value=this.value.replace(/\s/g,'');" id="jpin"
								type="text" name="jpin"></input><span style="color:red">*</span>
							</td>
						</tr>
						<tr style="height:25px;">
							<td class="td_tit">人员总数:</td>
							<td><input size="35" maxlength="12" style="width:250px;"
								onblur="this.value=this.value.replace(/\s/g,'');" id="userTotal"
								type="text" name="userTotal"/>
							</td>
						</tr>
						<tr style="height:25px;">
							<td class="td_tit">驻外部门:</td>
							<td><input size="35" maxlength="12" style="width:250px;"
								onblur="this.value=this.value.replace(/\s/g,'');" id="overDistrictName"
								type="text" name="overDistrictName"/>
								<input size="35" maxlength="12" style="width:250px;"
								onblur="this.value=this.value.replace(/\s/g,'');" id="overDistrictId"
								type="hidden" name="overDistrictId"/>
							</td>
						</tr>
					</table>
					<input type="hidden" name="parentId" id="parentId"
						value="${dept.deptId }">
					<c:choose>
						<c:when test="${'320500' eq dept.deptId}">
							<input type="hidden" id="deep" name="deep" value="2">
						</c:when>
						<c:otherwise>
							<input type="hidden" id="deep" name="deep"
								value="${dept.deep+1 }">
						</c:otherwise>
					</c:choose>
					<input type="hidden" name="deptId" id="longId">
				</form>
			</div>
		</c:otherwise>
	</c:choose>
</body>
</html>